package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MemberDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private UserDao userDao;
    
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<Member> memberList;
        if (queryString == null || "".equals(queryString)) {
            memberList = memberDao.getAllMember();
        } else {
            memberList = memberDao.getByQueryString(queryString);
        }
        PageInfo pageInfo = new PageInfo<>(memberList);
        return new PageResult(pageInfo.getTotal(),memberList);
    }

    @Override
    public Result getUsers() {
        try {
            List<User> userList = userDao.findPage();
            return new Result(true,"查询健康管理师数据成功",userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询健康管理师数据失败");
        }
    }

    @Override
    public Result add(Member member) {
        try {
            member.setRegTime(new Date());
            memberDao.insertMember(member);

            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    @Override
    public Result findById(Integer id) {
        try {
            Member member = memberDao.findById(id);
            return new Result(true,"查询会员详细信息成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询会员详细信息失败");
        }
    }

    @Override
    public Result edit(Member member) {
        try {
            memberDao.updateMember(member);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    @Override
    public Result delete(Integer id) {
        try {
            memberDao.deleteMember(id);
            return new Result(true,"删除数据成功");
        } catch(Exception e) {
            e.printStackTrace();
            return new Result(false,"删除数据失败");

        }

    }

    @Override
    public Member getMemberById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public List<Setmeal> findByMemberId(Integer memberId) {
        return memberDao.findByMemberId(memberId);
    }

    @Override
    public List<CheckGroup> findByCheckGroup(Integer setmaelId) {
        return memberDao.findByCheckGroup(setmaelId);
    }

    @Override
    public List<CheckItem> findByCheckItem(Integer checkGroupId) {
        return memberDao.findByCheckItem(checkGroupId);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }


}











